package cn.fl.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.IPrdTkInfoService;
import cn.fl.product.transform.PrdTkInfoVOTransform;
import cn.fl.product.vo.PrdTkInfoVO;
import fl.product.facade.product.api.IProductApiService;
import fl.product.facade.product.dto.PrdTkInfoDTO;

/**
 * @Description: 卡车产品信息表 客户端服务层接口实现
 * @Description: 持久层方法名称前缀：新增：insert* 删除：delete* 修改：update* 批量：batch* 查询对象：select* get* find* 查询集合：select*
 */
@Service
public class PrdTkInfoServiceImpl implements IPrdTkInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(PrdTkInfoServiceImpl.class);
	
	/**
	 * 产品 API 暴露服务
	 */
	@Autowired
	private IProductApiService productApiService;
	
	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public PrdTkInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "根据主键查询卡车信息失败,主键参数为空.ID:"+id,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		PrdTkInfoDTO dto = this.productApiService.selectPrdTkInfoById(id, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		PrdTkInfoVO prdTkInfoVO = PrdTkInfoVOTransform.toVO(dto);
		//金额处理
		if(null!=prdTkInfoVO){
			if(null!=prdTkInfoVO.getClsExfPc()){
				prdTkInfoVO.setClsExfPcDouble(DecimalUtils.convertFenToYuan(prdTkInfoVO.getClsExfPc()));
			}
			if(null!=prdTkInfoVO.getGuidePc()){
				prdTkInfoVO.setGuidePcDouble_tk(DecimalUtils.convertFenToYuan(prdTkInfoVO.getGuidePc()));
			}
		}
		return prdTkInfoVO;
	}

	@Override
	public Long add(PrdTkInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增卡车失败,卡车对象参数为空.DATA:"+com.alibaba.fastjson.JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.addPrdTkInfo(PrdTkInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		//return this.prdTkInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除卡车失败,主键参数为空.IDS:"+com.alibaba.fastjson.JSONObject.toJSONString(ids),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.removePrdTkInfoByIds(ids);
    }

	@Override
	public boolean modify(PrdTkInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改卡车失败,卡车对象参数为空.DATA:"+com.alibaba.fastjson.JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.modifyPrdTkInfo(PrdTkInfoVOTransform.toDTO(entity));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.productApiService.searchPrdTkInfoListPage(page, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		List<PrdTkInfoDTO> resultList = (List<PrdTkInfoDTO>) page.getResultList();
		List<PrdTkInfoVO> voList = PrdTkInfoVOTransform.toVOList(resultList);
		page.setResultList(voList);
		return page;
	}

	@Override
	public boolean modifyEnAblePrdTkInfo(PrdTkInfoVO entity) throws BaseException {
		if(null == entity || entity.getId()==null || entity.getIsValid()==null){
			BaseLogUtils.info(logger, "modifyEnAblePrdTkInfo", "启用卡车信息失败,缺少必要的参数(卡车ID和启用标识).DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.modifyEnAblePrdTkInfo(PrdTkInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean modifyDisAblePrdTkInfo(PrdTkInfoVO entity) throws BaseException {
		if(null == entity || entity.getId()==null || entity.getIsValid()==null){
			BaseLogUtils.info(logger, "modifyDisAblePrdTkInfo", "禁用卡车信息失败,缺少必要的参数(卡车ID和禁用标识).DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.modifyDisAblePrdTkInfo(PrdTkInfoVOTransform.toDTO(entity));
	}

	@Override
	public List<PrdTkInfoVO> getPrdTkInfoByPrdId(Long id) throws BaseException {
		if(null==id) return null;
		return  PrdTkInfoVOTransform.toVOList(productApiService.getPrdTkInfoByPrdId(id));
	}

	@Override
	public Pager getPrdTKInfoPage(Pager page,String sysCd,String orgCd) throws BaseException {
		if(null==page) return new Pager();
		return productApiService.getPrdTKInfoPage(page,sysCd,orgCd);
	}
	
	
	//******************************私有方法*************************************/
	/**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	@SuppressWarnings("unused")
	private List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "getKvList", "获取字典项组"+grpCd+"失败.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getKvList", "获取字典项组"+grpCd+"失败,系统错误.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return kvList;
	}

	@Override
	public boolean modifyAudStsCd(Long id, String audStsCd, Date mdfTm,
			Long mdfUsrId) throws BaseException {
		if(StringUtils.isEmpty(id)||StringUtils.isEmpty(audStsCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.modifyPrdTkInfoAudStsCd(id, audStsCd, mdfTm, mdfUsrId);
	}

}
